//
//  HLIMKitSessionTableAdapter.m
//  YueYue
//
//  Created by hulu001 on 2024/1/11.
//

#import "HLIMKitSessionTableAdapter.h"
#import "HLIMKitMessageCellFactory.h"
#import "HLIMKitTimeModel.h"
#import "HLIMKitSystemMessageModel.h"


@interface HLIMKitSessionTableAdapter ()

@property (nonatomic, strong) HLIMKitMessageCellFactory *cellFactory;

@end

@implementation HLIMKitSessionTableAdapter


- (instancetype)init
{
    self = [super init];
    
    if (self) {
        _cellFactory = [[HLIMKitMessageCellFactory alloc] init];
    }
    
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.interactor.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    id model = [self.interactor.items objectAtIndex:indexPath.row];
    
    if ([model isKindOfClass:[HLIMKitMessageModel class]])
    {
        cell = [self.cellFactory cellInTable:tableView forMessageMode:model];
        [(HLIMKitMesssageCell *)cell setDelegate:self.delegate];
    }
    else if ([model isKindOfClass:HLIMKitTimeModel.class])
    {
        cell = [self.cellFactory cellInTable:tableView forTimeMode:model];
    }
    else if ([model isKindOfClass:HLIMKitSystemMessageModel.class])
    {
        cell = [self.cellFactory cellInTable:tableView forSystemModel:model];
    }
    else
    {
        NSAssert(1, @"not support model");
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cellHeight = 0.01;
    id modelInArray = [[self.interactor items] objectAtIndex:indexPath.row];
    
    if ([modelInArray isKindOfClass:[HLIMKitMessageModel class]])
    {
        HLIMKitMessageModel *model = (HLIMKitMessageModel *)modelInArray;
        
        CGSize size = [model contentSize:tableView.width];
        CGFloat avatarMarginY = [model avatarMargin].y;

        UIEdgeInsets contentViewInsets = model.contentViewInsets;
        UIEdgeInsets bubbleViewInsets  = model.bubbleViewInsets;
        cellHeight = size.height + contentViewInsets.top + contentViewInsets.bottom + bubbleViewInsets.top + bubbleViewInsets.bottom;
        if (cellHeight <= (model.avatarSize.height + avatarMarginY)) {
            cellHeight = model.avatarSize.height + avatarMarginY;
//            [QMUIConsole log:[NSString stringWithFormat:@"msg:%@\nHeight:%.1f", model.message.text ,cellHeight]];
        }
        
        if ([model needShowRepliedContent]) {
            CGSize replyedSize = [model replyContentSize:tableView.width];
            cellHeight = cellHeight + replyedSize.height + 4;
        }
//        [QMUIConsole log:[NSString stringWithFormat:@"msg:%@\nHeight:%.1f", model.message.text ,cellHeight]];
    }
    else if ([modelInArray isKindOfClass:[HLIMKitTimeModel class]])
    {
        cellHeight = [(HLIMKitTimeModel *)modelInArray height];
//        [QMUIConsole log:[NSString stringWithFormat:@"msg:时间消息\nHeight:%.1f", cellHeight]];
    }
    else if ([modelInArray isKindOfClass:[HLIMKitSystemMessageModel class]])
    {
        cellHeight = [(HLIMKitSystemMessageModel *)modelInArray height];
//        [QMUIConsole log:[NSString stringWithFormat:@"msg:系统消息\nHeight:%.1f", cellHeight]];
    }
    else
    {
        NSAssert(0, @"not support model");
//        [QMUIConsole log:@"msg:异常\nHeight:0"];
    }
    
    return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat height = scrollView.frame.size.height;
    CGFloat contentYoffset = scrollView.contentOffset.y;
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;
    if (distanceFromBottom <= height + 5) {
        self.interactor.scrollToBottomStatus = 1;
    } else {
        self.interactor.scrollToBottomStatus = 2;
    }
}


@end
